package com.ehouse.adapters;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.ehouse.MainActivity;
import com.ehouse.R;
import com.ehouse.connection.ConnectionService;
import com.ehouse.connection.ConnectionService.IConnectionResponseListener;
import com.ehouse.database.TemperatureTable;
import com.ehouse.models.Settings;

public class TemperatureItemAdapter extends ResourceCursorAdapter{
	private int layout;
	private boolean heaterState = false;
	private boolean acState = false;
 
	public TemperatureItemAdapter (Context context, int layout, Cursor c, int flags){
		super(context, layout, c, flags);
		this.layout = layout;
	}

	    @Override
	    public View newView(Context context, Cursor cur, ViewGroup parent) {
	        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        return li.inflate(layout, parent, false);
	    }

	    @Override
	    public void bindView(View view, final Context context, Cursor cursor) {
	    	int temp = cursor.getInt(cursor.getColumnIndex(TemperatureTable.COLUMN_TEMP));
	    	
	    	final SharedPreferences sharedPrefs = PreferenceManager
					.getDefaultSharedPreferences(context);
	    	
			boolean fahrenheit = (sharedPrefs.getString(Settings.TEMP_UNIT, "0").equals("1"));
	    	String unit = context.getResources().getString(R.string.unit_c);
			if(fahrenheit){
				temp =  (((temp/5)*9)+32);
				unit = context.getResources().getString(R.string.unit_f);
			}
			
			
	    	String label_h = cursor.getString(cursor.getColumnIndex(TemperatureTable.COLUMN_LABEL_H));
	    	boolean state_h = (cursor.getInt(cursor.getColumnIndex(TemperatureTable.COLUMN_STATE_H)) == 1);
	    	
	    	String label_a = cursor.getString(cursor.getColumnIndex(TemperatureTable.COLUMN_LABEL_A));
	    	boolean state_a = (cursor.getInt(cursor.getColumnIndex(TemperatureTable.COLUMN_STATE_A)) == 1);
	    	
	    	TextView tempLabel = (TextView)view.findViewById(R.id.tempLabel);
	    	tempLabel.setText(context.getResources().getString(R.string.temp_label) + " " + String.valueOf(temp) + " " +unit);
	    	
	    	TextView heatingLabel = (TextView)view.findViewById(R.id.heatingLabel);
            ToggleButton heatingButton = (ToggleButton)view.findViewById(R.id.heatingButton);
            
            if(state_h) {
            	heaterState = true;
            }
            if(state_a) {
            	acState = true;
            }
            
            heatingLabel.setText(label_h);
            heatingButton.setChecked(state_h);
            heatingButton.setTag(label_h);
            
	    	TextView acLabel = (TextView)view.findViewById(R.id.acLabel);
            ToggleButton acButton = (ToggleButton)view.findViewById(R.id.acButton);
            
            acLabel.setText(label_a);
            acButton.setChecked(state_a);
            acButton.setTag(label_a);
            
            OnCheckedChangeListener listener = new OnCheckedChangeListener() {
    			
    			public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
    				String label = (String)buttonView.getTag();
    				
    				if (label.equals("Heating") && acState) {
    					buttonView.setChecked(false);
    					return;
    				}else if(label.equals("Heating") && !acState){
    					heaterState = true;
    					acState = false;
    				}
    				if (label.equals("AC") && heaterState) {
    					buttonView.setChecked(false);
    					return;
    				}else if(label.equals("AC") && !heaterState){
    					heaterState = false;
    					acState = true;
    				}
    				
    				
    				MainActivity.serviceRef.switchSensor("Temperature", label, isChecked, new ConnectionService.IConnectionResponseListener() {
    					
    					public void responseReceived(Object data, boolean success) {
    						if(success){
    							MainActivity.serviceRef.getTemperature(new IConnectionResponseListener() {
    								
    								public void responseReceived(Object data, boolean success) {
    									if(!success){
    										Toast.makeText(context,
    												R.string.connection_error, Toast.LENGTH_LONG)
    												.show();
    		
    									}
    								}
    							});
    						}else{
    							Toast.makeText(context,
    									R.string.connection_error, Toast.LENGTH_LONG)
    									.show();

    						}
    					}
    				
    				});
    			}
    			
    		};
            
            acButton.setOnCheckedChangeListener(listener);
            
            heatingButton.setOnCheckedChangeListener(listener);
	    }
}